<?php

/**
 * Gestionnaire du systeme de Parsing de Template
 *
 * @category   McCube
 * @package    Template\Parser
 * @author     Leiha Sellier <leiha.sellier@gmail.com>
 * @license    http://creativecommons.org/licenses/by/3.0/  CC BY 3.0
 * @link       http://code.google.com/p/mechanics-cubes/
 */
class McCube_Template_Parser{

    public    static $delimiters = array('{', '}');
    protected static $_listeners = array();

    /**
     * Setter for tag delimiters
     *
     * @param string $start
     * @param string $end
     * @return void
     */
    public static function setDelimiters($start, $end){
        self::$delimiters = array($start, $end);
    }

    /**
     *
     *
     * @return int
     */
    public static function addExecListener(){
        self::$_listeners[] = true;
        return count(self::$_listeners);
    }

    /**
     *
     *
     * @return array
     */
    public static function getListeners(){
        return self::$_listeners;
    }

    public $tplEngine = null;

    public function __construct($tplEngine=null){
        $this->tplEngine = $tplEngine;
    }

    /**
     * Parse the content of file for find elements of template
     *
     * @param string $filename
     * @param array  $varsForBuffer
     * @return string
     */
    public function parseFile($filename, $varsForBuffer=array()){
        return $this->parseString(file_get_contents($filename), $varsForBuffer);
	}

    /**
     * Parse a string for find elements of template
     *
     * @return string
     */
	public function parseString($string, $varsForBuffer=array()){
	    $string = $this->getStringParser  ()->parse($string);
        $string = $this->getVariableParser()->parse($string);
        $string = $this->getCommandParser ()->parse($string);
        $string = $this->getHelperParser  ()->parse($string);
        return $string;
    }

    /**
     * Getter for String Element Parser
     *
     * @return object
     */
    public function getStringParser(){
        return new McCube_Template_Parser_Element_String($this);
    }

    /**
     * Getter for Variable Element Parser
     *
     * @return object
     */
    public function getVariableParser(){
        return new McCube_Template_Parser_Element_Variable($this);
    }

	/**
     * Getter for Command Element Parser
     *
     * @return object
     */
    public function getCommandParser(){
        return new McCube_Template_Parser_Element_Command($this);
	}

	/**
     * Getter for Helper Element Parser
     *
     * @return object
     */
    public function getHelperParser(){
        return new McCube_Template_Parser_Element_Helper($this);
	}

}